package com.ruoyi.web.controller.monitor;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SocietyManage;
import com.ruoyi.system.domain.SysUserSociety;
import com.ruoyi.system.mapper.SocietyManageMapper;
import com.ruoyi.system.mapper.SysUserSocietyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysOperLog;
import com.ruoyi.system.service.ISysOperLogService;

/**
 * 操作日志记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/operlog")
public class SysOperlogController extends BaseController {
    @Autowired
    private ISysOperLogService operLogService;

    @Autowired
    private SocietyManageMapper societyManageMapper;

    @GetMapping("/list")
    public TableDataInfo list(SysOperLog operLog) {
        startPage();

        List<SysOperLog> list = operLogService.selectOperLogList(operLog);

        if (StringUtils.isNotBlank(operLog.getSocietyName())){
            list.forEach(item -> {
                String operParam = item.getOperParam();
                if (operParam.length()<10){
                    int i = item.getOperParam().indexOf("=");
                    String substring = item.getOperParam().substring(i+1, item.getOperParam().length() - 1);
                    SocietyManage userSociety = societyManageMapper.selectSocietyManageById(Long.parseLong(substring));
                    item.setSocietyName(userSociety.getSocietyName());
                }else {
                    SysUserSociety sysUserSociety = JSON.parseObject(operParam, SysUserSociety.class);
                    SocietyManage userSociety = societyManageMapper.selectSocietyManageById(sysUserSociety.getSocietyId());
                    item.setSocietyName(userSociety.getSocietyName());
                }

            });
        }
        return getDataTable(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog) {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{operIds}")
    public AjaxResult remove(@PathVariable Long[] operIds) {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        operLogService.cleanOperLog();
        return AjaxResult.success();
    }
}
